package com.goldplusgold.tradedelay.app.home.live.base;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.goldplusgold.live.bean.BaseBean;
import com.goldplusgold.support.lib.base.activity.BaseActivity;
import com.goldplusgold.tradedelay.app.base.constant.ErrorCodeConstant;
import com.goldplusgold.tradedelay.app.base.utils.ActivityUtils;

import java.lang.ref.WeakReference;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Administrator on 2017/8/17.
 */

public class LiveBaseCallBack<T> implements Callback<T>{

    private WeakReference<Context> context ;

    public LiveBaseCallBack(Context context){
        this.context = new WeakReference<>(context) ;
    }

    @Override
    public void onResponse(Call<T> call, Response<T> response) {
        if(context == null) {
            return;
        }
        Context c = context.get();
        if(c == null){
            return;
        }
        if(c instanceof Activity){
            if(((Activity) c).isFinishing()){
                return;
            }else{
                T t = response.body();
                if(t instanceof BaseBean){
                    if(TextUtils.equals( ((BaseBean)t).getErrorCode() , ErrorCodeConstant._EXTRUSION_SIGN_IN_CODE)){
                        String message = ((BaseBean)t).getErrorMsg() ;
                        ActivityUtils.isQuit((Activity)c, message);
                        return;
                    }
                }
            }
        }
    }

    @Override
    public void onFailure(Call<T> call, Throwable t) {
    }

}
